//
//  MKUIElementTextInputViewController.m
//  MKUI
//
//  Created by 苏尚进 on 2023/7/13.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUIElementTextInputViewController.h"
#import "MKUIPreCompile.h"

@interface MKUIElementTextInputViewController (){
    BOOL _NeedReloadData;
}
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic,strong) MKUIListView *listView;
@property(nonatomic,strong) MKUIElementListViewCellTextField *textFieldElement;
@property(nonatomic,strong) MKUIElementListViewCellTextArea *textAreaElement;
@end

@implementation MKUIElementTextInputViewController
#ifdef DEBUG
- (void)dealloc{
    
}
#endif
- (void)viewDidLoad {
    [super viewDidLoad];
    if(_doneButtonText.length==0){
        _doneButtonText = @"Done";
    }
    self.listView = [[MKUIListView alloc] initWithFrame:CGRectZero style:(MKUIListViewStyleInsetGrouped)];
    self.listView.listHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 20)];
    [self.view addSubview:self.listView];
    
    _NeedReloadData = YES;
    //
    self.doneButton = [[UIButton alloc] init];
    [self.doneButton setTitle:self.doneButtonText forState:(UIControlStateNormal)];
    [self.doneButton setTitleColor:[UIColor systemBlueColor] forState:(UIControlStateNormal)];
    self.doneButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.doneButton addTarget:self action:@selector(__onDoneButtonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self.doneButton sizeToFit];
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithCustomView:self.doneButton];
    self.navigationItem.rightBarButtonItem = doneItem;
}
- (void)__onDoneButtonTap:(id)sender{
    if(self.whenDone){
        self.whenDone(self);
    }
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.view.bounds;
    self.listView.frame = bounds;
    if(_NeedReloadData){
        _NeedReloadData = NO;
        [self reloadData];
    }
}
- (void)setNeedReloadData{
    _NeedReloadData = YES;
    [self.view setNeedsLayout];
}
- (void)setDoneButtonText:(NSString *)doneButtonText{
    _doneButtonText = doneButtonText;
    self.textAreaElement.elementView.doneButtonText = doneButtonText;
}
- (void)setText:(NSString *)text{
    if(_text==text||[_text isEqualToString:text])return;
    _text = text;
    [self setNeedReloadData];
}
- (void)setAllowsMultipleLineText:(BOOL)allowsMultipleLineText{
    if(_allowsMultipleLineText==allowsMultipleLineText)return;
    _allowsMultipleLineText = allowsMultipleLineText;
    [self setNeedReloadData];
}
- (void)reloadData{
    [self.listView deleteAllSections];
    if(self.allowsMultipleLineText){
        self.textAreaElement.elementView.text = self.text;
        self.textAreaElement.elementView.doneButtonText = self.doneButtonText;
        [self.listView addItem:self.textAreaElement];
    }else{
        self.textFieldElement.elementView.text = self.text;
        [self.listView addItem:self.textFieldElement];
    }
    [self.listView reloadData];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if(self.allowsMultipleLineText){
        [self.textAreaElement.elementView.textView becomeFirstResponder];
    }else{
        [self.textFieldElement.elementView.textField becomeFirstResponder];
    }
}
- (MKUIElementListViewCellTextField *)textFieldElement{
    if(_textFieldElement) return _textFieldElement;
    MKUIElementListViewCellTextField *el = [[MKUIElementListViewCellTextField alloc] init];
    el.elementView.textField.textAlignment = NSTextAlignmentLeft;
    @MK_WEAKIFY(self);
    el.elementView.whenReturn = ^(MKUIElementViewTextField * _Nonnull el) {
        @MK_NORMALIZE(self);
        if(self.whenDone){
            self.whenDone(self);
        }
    };
    el.elementView.whenChange = ^(__kindof MKUIElementViewTextField * _Nonnull el, NSString * _Nonnull text) {
        @MK_NORMALIZE(self);
        self->_text = text;
        if(self.whenChange){
            self.whenChange(self, text);
        }
    };
    _textFieldElement = el;
    return _textFieldElement;
}
- (MKUIElementListViewCellTextArea *)textAreaElement{
    if(_textAreaElement)return _textAreaElement;
    MKUIElementListViewCellTextArea *el = [[MKUIElementListViewCellTextArea alloc] init];
    el.elementView.textView.textAlignment = NSTextAlignmentLeft;
    @MK_WEAKIFY(self);
    el.elementView.whenDone = ^(__kindof MKUIElementViewTextArea * _Nonnull el) {
        @MK_NORMALIZE(self);
        if(self.whenDone){
            self.whenDone(self);
        }
    };
    el.elementView.whenChange = ^(__kindof MKUIElementViewTextArea * _Nonnull el, NSString * _Nonnull text) {
        @MK_NORMALIZE(self);
        self->_text = text;
        if(self.whenChange){
            self.whenChange(self, text);
        }
        [self.textAreaElement mk_reloadInListView];
    };
    _textAreaElement = el;
    return _textAreaElement;
}
@end
